﻿using ff14bot.Enums;
using ff14bot.Managers;
using TreeSharp;
using Magitek.Helpers;
using ff14bot;
using ff14bot.Objects;
using ff14bot.Behavior;

namespace Magitek.Rotations
{
    public class Dragoon : Magitek
    {
        public override float PullRange { get { return WindowSettings.DragoonPullRange; } }
        public override ClassJobType[] Class { get { return new ClassJobType[] { ClassJobType.Dragoon }; } }

        #region Pulling & Resting

        protected override Composite CreatePull()
        {
            return Combat.CreatePull(2);
        }

        protected override Composite CreateRest()
        {
            return Extensions.DefaultRestBehavior(r => Core.Player.CurrentManaPercent);
        }
        #endregion
        #region Buffs // Mainly used for Grindbot etc...

        protected override Composite CreatePreCombatBuffs()
        {
            return new PrioritySelector(
                Extensions.SummonChocobo()
                );
        }

        protected override Composite CreateCombatBuffs()
        {
            return new PrioritySelector(
                Casting.Cast(r => "Power Surge", r => WindowSettings.DragoonUseJump && Core.Player.HasAura("Blood for Blood") && Core.Player.HasAura("Internal Release"), r => Core.Player, r => 2, r => false),
                Casting.Cast(r => "Life Surge", r => Core.Player.HasAura("Blood for Blood") && Core.Player.HasAura("Internal Release") && Actionmanager.LastSpell.Name == "Vorpal Thrust", r => Core.Player, r => 2, r => false),
                Casting.Cast(r => "Invigorate", r => Core.Player.CurrentTPPercent < WindowSettings.DragoonUseInvigorateTp, r => Core.Player, r => 2, r => false),
                Casting.Cast(r => "Blood for Blood", r => Core.Player.CurrentTarget.HasAura("Disembowel") && Core.Player.CurrentTarget.HasAura("Chaos Thrust") && WindowSettings.DragoonUseBuffs, r => Core.Player, r => 2, r => false),
                Casting.Cast(r => "Internal Release", r => Core.Player.HasAura("Blood for Blood") && Core.Player.CurrentTarget.HasAura("Disembowel") && Core.Player.CurrentTarget.HasAura("Chaos Thrust") && WindowSettings.DragoonUseBuffs, r => Core.Player, r => 2, r => false),

                Casting.Cast(r => "Power Surge", r => WindowSettings.DragoonUseJump && !WindowSettings.DragoonSaveBuffs, r => Core.Player, r => 2, r => false),
                Casting.Cast(r => "Life Surge", r => !WindowSettings.DragoonSaveBuffs, r => Core.Player, r => 2, r => false),
                Casting.Cast(r => "Blood for Blood", r => !WindowSettings.DragoonSaveBuffs && WindowSettings.DragoonUseBuffs, r => Core.Player, r => 2, r => false),
                Casting.Cast(r => "Internal Release", r => !WindowSettings.DragoonSaveBuffs && WindowSettings.DragoonUseBuffs, r => Core.Player, r => 2, r => false)
                );
        }

        #endregion
        #region Combat
        protected override Composite CreateHeal()
        {
            return new PrioritySelector(
                Casting.Cast(r => "Second Wind", r => Core.Player.CurrentHealthPercent <= WindowSettings.DragoonUseSecondWindHp, r => Core.Player, r => 2, r => false),
                Casting.Cast(r => "Bloodbath", r => Core.Player.CurrentHealthPercent <= WindowSettings.DragoonUseBloodbathHp, r => Core.Player, r => 2, r => false)
            );
        }

        protected override Composite CreateCombat()
        {
            return new PrioritySelector(ctx => Core.Player.CurrentTarget,
                new Decorator(ctx => ctx != null,
                new PrioritySelector(
                new Decorator(r => BotManager.Current.IsAutonomous, new PrioritySelector(
                CommonBehaviors.MoveToLos(ctx => ctx as GameObject),
                CommonBehaviors.MoveAndStop(ctx => (ctx as GameObject).Location, ctx => 3 + (ctx as BattleCharacter).CombatReach, true, "[Magitek] Moving in Range"))),

                //Casting.Cast(r => "Leg Sweep", r => WindowSettings.InterruptCast && Combat.CheckForInterrupt(), r => Core.Player.CurrentTarget, r => 2, r => false),

                Combat.DoPVP(),

                #region AOE Mode
                new Decorator(r => WindowSettings.AOEEnabled && Core.Player.CurrentTarget.IsFlanking, new PrioritySelector(
                    Casting.Cast(r => "Dragonfire Dive", r => WindowSettings.DragoonUseDragonfireDive, r => Core.Player.CurrentTarget, r => 3, r => false),
                    Casting.Cast(r => "Ring of Thorns", r => Actionmanager.LastSpell.Name == "Heavy Thrust", r => Core.Player, r => 3, r => false),
                    Casting.Cast(r => "Heavy Thrust", r => true, r => Core.Player.CurrentTarget, r => 3, r => true))),
                #endregion

                new Decorator(ret =>
                {
                    if (!WindowSettings.DragoonUseAOE)
                        return false;

                    if (WindowSettings._dontaoeList.Contains(Core.Player.CurrentTarget.Name))
                        return false;

                    if (Extensions.EnemiesNearTarget(5) >= WindowSettings.TargetsToAOE && Core.Player.CurrentTarget.IsFlanking)
                        return true;

                    return false;
                }, new PrioritySelector(
                    Casting.Cast(r => "Dragonfire Dive", r => WindowSettings.DragoonUseDragonfireDive, r => Core.Player.CurrentTarget, r => 3, r => false),
                    Casting.Cast(r => "Ring of Thorns", r => Actionmanager.LastSpell.Name == "Heavy Thrust", r => Core.Player, r => 3, r => false),
                    Casting.Cast(r => "Heavy Thrust", r => true, r => Core.Player.CurrentTarget, r => 3, r => true)
                    )),

                Casting.Cast(r => "Full Thrust", r => Core.Player.HasAura("Life Surge") || WindowSettings.lastspellused == "Vorpal Thrust" || WindowSettings.lastspellused == "Life Surge", r => Core.Player.CurrentTarget, r => 2, r => false),

                Casting.Cast(r => "Dragonfire Dive", r => WindowSettings.DragoonUseAOE && WindowSettings.DragoonUseDragonfireDive && Core.Player.HasAura("Blood for Blood") && Core.Player.HasAura("Internal Release") && Actionmanager.LastSpell.Name == "Full Thrust", r => Core.Player.CurrentTarget, r => 2, r => false),
                Casting.Cast(r => "Spineshatter Dive", r => WindowSettings.DragoonUseSpineshatterDive && !WindowSettings.DragoonUseAOE && Core.Player.HasAura("Blood for Blood") && Core.Player.HasAura("Internal Release") && Actionmanager.LastSpell.Name == "Full Thrust", r => Core.Player.CurrentTarget, r => 2, r => false),

                Casting.Cast(r => "Jump", r => Core.Player.HasAura("Power Surge") && WindowSettings.DragoonUseJump, r => Core.Player.CurrentTarget, r => 2, r => false),

                Casting.Cast(r => "Leg Sweep", r => !WindowSettings.InterruptCast, r => Core.Player.CurrentTarget, r => 2, r => false),
                
                Casting.Cast(r => "Vorpal Thrust", r => Core.Player.HasAura("Blood for Blood") && Core.Player.HasAura("Internal Release") && Actionmanager.LastSpell.Name == "True Thrust", r => Core.Player.CurrentTarget, r => 2, r => false),
                Casting.Cast(r => "True Thrust", r => Core.Player.HasAura("Blood for Blood") && Core.Player.HasAura("Internal Release"),r => Core.Player.CurrentTarget, r => 2, r => false),
                Casting.Cast(r => "Mercy Stroke", r => !Core.Player.HasAura("Life Surge"), r => Core.Player.CurrentTarget, r => 2, r => false),

                new Decorator(ret =>
                {

                    if (Actionmanager.LastSpell.Name == "Impulse Drive" || Actionmanager.LastSpell.Name == "Disembowel")
                        return true;

                    return false;

                }, new PrioritySelector(

                    Casting.Cast(r => "Chaos Thrust", r => Actionmanager.LastSpell.Name == "Disembowel", r => Core.Player.CurrentTarget, r => 4, r => false),
                    Casting.Cast(r => "Disembowel", r => Actionmanager.LastSpell.Name == "Impulse Drive", r => Core.Player.CurrentTarget, r => 4, r => false))),

                Casting.Cast(r => "Impulse Drive", r => !Core.Player.CurrentTarget.HasAura("Chaos Thrust", true, 3000) && Core.Player.CurrentTarget.IsBehind, r => Core.Player.CurrentTarget, r => 3, r => true),

                new Decorator(ret =>
                {
                    if (Actionmanager.LastSpell.Name == "True Thrust")
                        return true;

                    if (Actionmanager.LastSpell.Name == "Vorpal Thrust")
                        return true;

                    return false;

                }, new PrioritySelector(
                    Casting.Cast(r => "Full Thrust", r => Actionmanager.LastSpell.Name == "Vorpal Thrust" && WindowSettings.lastspellused == "Vorpal Thrust", r => Core.Player.CurrentTarget, r => 2, r => false),
                    Casting.Cast(r => "Vorpal Thrust", r => Actionmanager.LastSpell.Name == "True Thrust", r => Core.Player.CurrentTarget, r => 2, r => false)
                    )),

                Casting.Cast(r => "Heavy Thrust", r => Core.Player.CurrentTarget.IsFlanking && (!Core.Player.HasAura("Heavy Thrust", true, msLeft: 3000) || !Core.Player.HasAura("Heavy Thrust")), r => Core.Player.CurrentTarget, r => 3, r => true),
                Casting.Cast(r => "Phlebotomize", r => !Core.Player.CurrentTarget.HasAura("Phlebotomize", true, msLeft: 2000), r => Core.Player.CurrentTarget, r => 3, r => true),
                Casting.Cast(r => "Fracture", r => !Core.Player.CurrentTarget.HasAura("Fracture", true), r => Core.Player.CurrentTarget, r => 3, r => true),
                Casting.Cast(r => "True Thrust", r => true, r => Core.Player.CurrentTarget, r => 3, r => false),
                Casting.Cast(r => "Piercing Talon", r => WindowSettings.DragoonUsePiercingTalon && Core.Player.CurrentTarget.Location.Distance(Core.Player.Location) > 10, r => Core.Player.CurrentTarget, r => 2, r => false)
           )));
        }
        
        #endregion
    }
}
